<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    // var p = new Promise(function (resolve, reject) {
    //     setTimeout(function () {
    //         resolve(1)
    //     }, 2000)
    // })
    // console.log(p); // {<pending>:undefined}


    // p.then(arg => {
    //     console.log("fulfilled", arg);
    // }, err => {
    //     console.log("rejected", err);
    // })




    // 链式请求1: 后面的需要依赖前面的写法
    // var p1 = getNumAsync(1, 2000);

    // p1.then(arg => {
    //     console.log("fulfilled", arg);
    //     return getNumAsync(2, 1000); // p2
    // }).then(arg => {
    //     console.log("fulfilled", arg);
    //     return getNumAsync(3, 3000); // p3
    // }).then(arg => {
    //     console.log("fulfilled", arg);
    // }).catch(err => {
    //     console.log("rejected", err);
    // })

    // 链式请求2: 并列执行,相互之间没有依赖关系
    var p1 = getNumAsync(1, 2000);
    var p2 = getNumAsync(2, 1000);
    var p3 = getNumAsync(3, 3000);

    var p = Promise.all([p1, p2, p3]);

    p.then(list => {
        console.log("fulfilled", list);
    }).catch(err => {
        console.log("rejected", err);
    })









    // 调用此方法 返回Promise对象(pending)  指定时间之后变为 fulfilled/rejected
    function getNumAsync(num = 0, delay = 0, isResolve = true) {
        return new Promise(function (resolve, reject) {
            setTimeout(function () {
                isResolve ? resolve(num) : reject(num);
            }, delay)
        })
    }
</script>

</html>